//
//  SEGApplyAuthorViewModel.m
//  SEGViewModel
//
//  Created by 山刘 on 2020/11/23.
//

#import "SEGApplyAuthorViewModel.h"

@interface SEGApplyAuthorViewModel()
@property (nonatomic, strong, readwrite) RACCommand *requestCommand;
@property (strong, nonatomic, readwrite) RACSubject *messageSubject;

@property (nonatomic, strong, readwrite) RACCommand *cancelApplyCommand;

@property (strong, nonatomic, readwrite) NSString *vcTitle;
@end

@implementation SEGApplyAuthorViewModel
#pragma mark - Init
-(instancetype)init{
    if (self = [super init]) {
        [self racInit];
    }
    return self;
}

-(void)dealloc{
    [self.requestCommand rac_deallocDisposable];
    [self.messageSubject rac_deallocDisposable];
    
    [self.cancelApplyCommand rac_deallocDisposable];
}

- (void)racInit {
    
    @weakify(self);
    _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            
            // Code here...
            [self isShowSVP:YES];
            
            [[SEGQueryauthinfoManager sharedManager] requestQueryauthinfoWithCommunityid:kAccountCommunityID finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                
                [self isShowSVP:NO];
                
                if (!isSuccessed) {
                    [subscriber sendNext:nil];
                    [subscriber sendCompleted]; // 39080 【Sany+1.3.2】iOS 用户提交申请认证成功，页面跳转显示错误；预期结果：跳转展示审核中页面
                    return;
                }
                
                SEGQueryauthinfoRoleauthmapModel *userInfo = [SEGQueryauthinfoManager sharedManager].QueryAuthInfo.data.userInfo;
                if (!userInfo || [userInfo.status isEqualToString:@""]) {
                    self.vcTitle = @"申请认证";
                    [subscriber sendNext:@"1"];
                    [subscriber sendCompleted]; // 39080 【Sany+1.3.2】iOS 用户提交申请认证成功，页面跳转显示错误；预期结果：跳转展示审核中页面
                    return;
                }
                
                [self isShowSVP:YES];
                self.vcTitle = @"企业认证";
                [[SEGUserselectbyuserManager sharedManager]requestUserselectbyuserWithCommunityid:kAccountCommunityID userId:kAccountUserID finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                    
                    [self isShowSVP:NO];
                    
                    if (!isSuccessed) {
                        [self showMessage:errorMessage];
                        [subscriber sendError:nil];
                        [subscriber sendCompleted]; // 39080 【Sany+1.3.2】iOS 用户提交申请认证成功，页面跳转显示错误；预期结果：跳转展示审核中页面
                        return;
                    }
                    
                    [subscriber sendNext:@"2"];
                    [subscriber sendCompleted];
                    return;
                }];
                
            }];
            
            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
    
    _cancelApplyCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            
            // Code here...
            [self isShowSVP:YES];
            SEGUserselectbyuserListModel *userSelectByUser = [SEGUserselectbyuserManager sharedManager].UserSelectByUser.data.list.firstObject;
            [[SEGUsercancelapplicationManager sharedManager] requestUsercancelapplicationWithEnterpriseuserid:userSelectByUser.enterpriseUserId finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                
                [self isShowSVP:NO];
                
                if (codeType && [codeType isEqualToString:@"-3"]) {
                    [subscriber sendNext:@{@"code":@"1",@"msg":errorMessage}];
                    [subscriber sendCompleted];
                    return;
                }
                
                if (!isSuccessed) {
                    [self showMessage:errorMessage];
                    [subscriber sendError:nil];
                    [subscriber sendCompleted];
                    return;
                }
                
                self.vcTitle = @"申请认证";
                [subscriber sendNext:@{@"code":@"2",@"msg":@""}];
                [subscriber sendCompleted];
                return;
                
            }];
            
            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
}

#pragma mark - Publish Methods

#pragma mark - Private Methods
-(void)isShowSVP:(BOOL)show {
    [self.messageSubject sendNext:@{@"type": show ? @"1" : @"2"}];
}
-(void)showMessage:(NSString*)msg {
    [self.messageSubject sendNext:@{@"type":@"3",@"msg":msg}];
}
#pragma mark - Getter
-(RACSubject *)messageSubject{
    if (!_messageSubject) {
        _messageSubject = [RACSubject subject];
    }
    return _messageSubject;
}
#pragma mark - Stter
@end
